/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.Love.service;

import com.fzhucc.exception.BadRequestException;
import com.fzhucc.modules.Love.dto.LoveDemandDto;
import com.fzhucc.modules.Love.dto.LoveModelDto;
import com.fzhucc.modules.loveDemandRecord.domain.LoveDemandRecord;
import com.fzhucc.modules.loveDemandRecord.domain.dto.LoveDemandRecordDto;
import com.fzhucc.modules.loveDemandRecord.repository.LoveDemandRecordRepository;
import com.fzhucc.modules.loveDemandRecord.service.mapstruct.LoveDemandRecordMapper;
import com.fzhucc.modules.loveModelRecord.domain.LoveModelRecord;
import com.fzhucc.modules.loveModelRecord.domain.dto.LoveModelRecordDto;
import com.fzhucc.modules.loveModelRecord.repository.LoveModelRecordRepository;
import com.fzhucc.modules.loveModelRecord.service.mapstruct.LoveModelRecordMapper;
import com.fzhucc.modules.sciPersonnelInfo.domain.SciPersonnelInfo;
import com.fzhucc.modules.sciPersonnelInfo.repository.SciPersonnelInfoRepository;
import com.fzhucc.modules.sciTechnologyDemand.domain.SciTechnologyDemand;
import com.fzhucc.modules.sciTechnologyDemand.repository.SciTechnologyDemandRepository;
import com.fzhucc.modules.system.repository.UserRepository;
import com.fzhucc.modules.system.service.UserService;
import com.fzhucc.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author yyg
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2024-06-12
 **/
@Service
@RequiredArgsConstructor
public class LoveService {

    private final LoveDemandRecordRepository loveDemandRecordRepository;
    private final LoveDemandRecordMapper loveDemandRecordMapper;

    private final SciTechnologyDemandRepository demandRepository;

    private final SciPersonnelInfoRepository modelRepository;

    private final LoveModelRecordRepository loveModelRecordRepository;
    private final LoveModelRecordMapper loveModelRecordMapper;

    private final UserRepository userRepository;

    private final UserService userService;

    @Transactional(rollbackFor = Exception.class)
    public LoveDemandRecordDto createDemandLove(LoveDemandDto dto) {
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            throw new BadRequestException("请先登录后进行操作");
        }
        Optional<SciTechnologyDemand> res = demandRepository.findById(dto.getDemandId());
        if (!res.isPresent()) {
            throw new BadRequestException("未找到该企业需求");
        }
        //TODO 如果收藏过则不在收藏
        List<LoveDemandRecord> list = loveDemandRecordRepository.findByDemanIdAndUserId(userId, dto.getDemandId());
        if (list != null && list.size() > 0) {
            throw new BadRequestException("该企业需求,您已经收藏过");
        }
        
        LoveDemandRecord loveDemandRecord = new LoveDemandRecord();
        loveDemandRecord.setUserId(userId);
        loveDemandRecord.setDemandId(dto.getDemandId());
        LoveDemandRecordDto loveDemandRecordDto = loveDemandRecordMapper.toDto(loveDemandRecordRepository.save(loveDemandRecord));

        if (loveDemandRecordDto != null) {
            //TODO 用户积分+5
            userService.updateFen(userId, 5);
        }
        return loveDemandRecordDto;
    }

    @Transactional(rollbackFor = Exception.class)
    public LoveModelRecordDto createModelLove(LoveModelDto dto) {
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            throw new BadRequestException("请先登录后进行操作");
        }
        Optional<SciPersonnelInfo> res = modelRepository.findById(dto.getModelId());
        if (!res.isPresent()) {
            throw new BadRequestException("未找到该劳模工匠");
        }
        //TODO 如果收藏过则不在收藏
        List<LoveModelRecord> list = loveModelRecordRepository.findByModelIdAndUserId(userId, dto.getModelId());
        if (list != null && list.size() > 0) {
            throw new BadRequestException("该劳模工匠,您已经收藏过");
        }
        
        LoveModelRecord loveModelRecord = new LoveModelRecord();
        loveModelRecord.setUserId(userId);
        loveModelRecord.setModelId(dto.getModelId());
        LoveModelRecordDto loveModelRecordDto = loveModelRecordMapper.toDto(loveModelRecordRepository.save(loveModelRecord));

        if (loveModelRecordDto != null) {
            //TODO 用户积分+5
            userService.updateFen(userId, 5);
        }
        return loveModelRecordDto;
    }

    public Boolean demandRecordIsLove(LoveDemandDto dto) {
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            throw new BadRequestException("请先登录后进行操作");
        }
        Optional<SciTechnologyDemand> res = demandRepository.findById(dto.getDemandId());
        if (!res.isPresent()) {
            throw new BadRequestException("未找到该企业需求");
        }
        List<LoveDemandRecord> list = loveDemandRecordRepository.findByDemanIdAndUserId(userId, dto.getDemandId());
        if (list != null && list.size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    public Boolean modelRecordIsLove(LoveModelDto dto) {
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            throw new BadRequestException("请先登录后进行操作");
        }
        Optional<SciPersonnelInfo> res = modelRepository.findById(dto.getModelId());
        if (!res.isPresent()) {
            throw new BadRequestException("未找到该劳模工匠");
        }
        List<LoveModelRecord> list = loveModelRecordRepository.findByModelIdAndUserId(userId, dto.getModelId());
        if (list != null && list.size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    public void deleteAllDemands(Integer demandId) {
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            throw new BadRequestException("请先登录后进行操作");
        }
        Optional<SciTechnologyDemand> res = demandRepository.findById(demandId);
        if (!res.isPresent()) {
            throw new BadRequestException("未找到该企业需求");
        }
        List<LoveDemandRecord> list = loveDemandRecordRepository.findByDemanIdAndUserId(userId, demandId);
        if (list != null && list.size() == 1) {
            loveDemandRecordRepository.deleteById(list.get(0).getId());
        } else {
            throw new BadRequestException("未符合正常的收藏记录,无法操作");
        }
    }

    public void deleteAllModel(Integer modelId) {
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            throw new BadRequestException("请先登录后进行操作");
        }
        Optional<SciPersonnelInfo> res = modelRepository.findById(modelId);
        if (!res.isPresent()) {
            throw new BadRequestException("未找到该劳模工匠");
        }
        List<LoveModelRecord> list = loveModelRecordRepository.findByModelIdAndUserId(userId, modelId);
        if (list != null && list.size() == 1) {
            loveModelRecordRepository.deleteById(list.get(0).getId());
        } else {
            throw new BadRequestException("未符合正常的收藏记录,无法操作");
        }
    }
}